package com.tb.attendancesystem.student;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.zxing.client.android.Utils.ZXingUtils;
import com.tb.attendancesystem.R;

/**
 * 学生的主活动类
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = MainActivity.class.getSimpleName();

    private Button btScan;              //扫码按钮
    private String sno;                 //学号

    //加载资源
    private void loadRes() {
        sno = getIntent().getStringExtra("userId");
        btScan = (Button) findViewById(R.id.bt_scan);
    }

    //注册监听
    private void register() {
        btScan.setOnClickListener(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_main);
        loadRes();
        register();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(btScan)) {           //打开扫码窗口
            //// TODO: 2016/12/7  
//            Intent openCameraIntent = new Intent(this, CaptureActivity.class);
//            startActivityForResult(openCameraIntent, 1);
            ZXingUtils.startCaptureForResult(this,1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //获取扫码结果
            String scanResult = data.getStringExtra("result_string");
            //去掉结尾的换行"\n"
            scanResult = scanResult.substring(0,scanResult.length() - 1);
            Log.d(TAG, scanResult);
            //转至结果页面
            startActivity(scanResult, sno);
        }
    }

    //跳转结果页面
    public void startActivity(String codeStr, String sno) {
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("result_string", codeStr);
        intent.putExtra("sno", sno);
        startActivity(intent);
    }

}
